package com.sptpc.springbootmybatis.controller;

import com.sptpc.springbootmybatis.model.Fication;
import com.sptpc.springbootmybatis.service.FicationService;
import com.sptpc.springbootmybatis.util.ServerResponse;
import com.sptpc.springbootmybatis.vo.FicationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/drink")
public class FicationController {
    @Autowired
    private FicationService ficationService;


    //新增分类
    @GetMapping(value = "/save", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ResponseBody
    public ServerResponse insert(FicationVo ficationVo) {
        return ServerResponse.createBySuccess(ficationService.saveFication(ficationVo));
    }

    @GetMapping("/delete")
    @ResponseBody
    public ServerResponse delete(@RequestParam(name = "id") Long id) {
        Fication fication = ficationService.findOne(id);
        if (fication != null) {
            if(ficationService.delete(id)){
                return ServerResponse.createBySuccessMessage("删除成功");
            }else {
                return ServerResponse.createBySuccessMessage("删除不成功");
            }
        } else {
            return ServerResponse.createByErrorMessage("找不到要删除的分类");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public ServerResponse update(FicationVo ficationVo) {
        return ServerResponse.createBySuccess(ficationService.update(ficationVo));
    }

}
